package org.example.Server.controller.survey;

import lombok.extern.slf4j.Slf4j;
import org.example.Server.mapper.paper.PaperPersonalMapper;
import org.example.Server.model.paper.PaperHeader;
import org.example.Server.model.survey.Survey;
import org.example.Server.model.survey.SurveyPaperDTO;
import org.example.Server.service.sruvey.SurveyService;
import org.example.Server.utils.ResultAPI;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 教师创建调查问卷
 */
@RestController
@Slf4j
@RequestMapping("/youxich/survey")
public class SurveyController {
    @Autowired
    private SurveyService surveyService;
    @Autowired
    PaperPersonalMapper paperPersonalMapper;

    /**
     * 创建问卷库的问卷头
     *
     * @param surveyPaperDTO
     * @return
     */
    @PostMapping
    public ResultAPI saveSurvey(@RequestBody SurveyPaperDTO surveyPaperDTO) {
        log.info("创建问卷SurveyDTO" + surveyPaperDTO);

        // 创建paper
        PaperHeader paperHeader = new PaperHeader();
        BeanUtils.copyProperties(surveyPaperDTO, paperHeader);
        paperPersonalMapper.addPaperHeader(paperHeader, 0);
        // 添加题型
        paperPersonalMapper.addPaperQsType(paperHeader.getPaper_id());
        // 创建survey_store
        Survey survey = new Survey();
        BeanUtils.copyProperties(surveyPaperDTO, survey);
        survey.setPaper_id(paperHeader.getPaper_id());
        survey.setIs_store(1);
        Boolean flag = surveyService.save(survey);

        return ResultAPI.success(survey);
    }

    /**
     * 获取问卷库调查问卷列表
     *
     * @param tech_id
     * @return
     */
    @GetMapping
    public ResultAPI getSurveyList(@RequestParam Integer tech_id) {
        return surveyService.getSurveyList(tech_id);
    }

}
